home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ASSEMBLER / FLOATENCODE / <tarZ$use> / files / floating2 / Article3 next >
Text File  |  1989-05-14  |  8KB  |  397 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.     Getting the Point......in BASIC 
  7.  
  8.      
  9.  
  10.  
  11.  
  12.     (c) Copyright TJ Chappell April 1989. 
  13.  
  14.  
  15.  
  16.          Acorn's latest version  of BBC BASIC has  improved many of
  17.  
  18.     the  features   of  previous   versions  and  added   many  new
  19.  
  20.     facilities.  However, the latest version  is still limited by a
  21.  
  22.     maximum of  9 decimal places  and maximum exponent of  only 38.
  23.  
  24.     These limits are normally acceptable for most users but what do
  25.  
  26.     you  do if  you  require  more accuracy  or  a  wider range  of
  27.  
  28.     numbers? 
  29.  
  30.  
  31.  
  32.          Until now the only possibilities  were to use a scientific
  33.  
  34.     calculator and enter the numbers into PRINT statements or write
  35.  
  36.     a machine code  program to use  the facilities of  the floating
  37.  
  38.     point unit.  Neither of these options offer a complete solution
  39.  
  40.     to the BASIC programmer. 
  41.  
  42.  
  43.  
  44.          The solution offered here removes the need to learn how to
  45.  
  46.     program the floating  point unit directly or  press any buttons
  47.  
  48.     on  a  calculator.  The  program  is  presented as  a  function
  49.  
  50.     library which can  be installed into the machine  in the normal
  51.  
  52.     way using the  INSTALL, LIBRARY or APPEND  command as required.
  53.  
  54.     It also relies upon the floating point emulator being installed
  55.  
  56.     as well. 
  57.  
  58.  
  59.  
  60.          Since BASIC  would round  numerical variables to  suit its
  61.  
  62.  
  63.  
  64.                                       Page 1
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     own  precision  it  is  necessary that  all  extended  accuracy
  73.  
  74.     numbers are  stored in string  variables.  The numbers  are all
  75.  
  76.     stored in the following format : 
  77.  
  78.  
  79.  
  80.          +/-m.mmmmmmmmmmmmmmmmmm E+/-eeee 
  81.  
  82.  
  83.  
  84.          This  leads to  numbers  with a  precision  of 18  decimal
  85.  
  86.     places and  a maximum exponent  of 9999 (somewhat  larger range
  87.  
  88.     than normal BASIC and most scientific calculators). 
  89.  
  90.  
  91.  
  92.          All variables  to be used  by the system must  observe the
  93.  
  94.     above format and  may be initialised to a  starting value using
  95.  
  96.     the FNCONST routine, e.g. 
  97.  
  98.  
  99.  
  100.          number$=FNCONST("1.234567") 
  101.  
  102.          value$=FNCONST("3.101E69") 
  103.  
  104.          old$=FNCONST(STR$(old)) 
  105.  
  106.  
  107.  
  108.          The  FNCONST routine  will accept  numbers in  any of  the
  109.  
  110.     above forms.  Note that in  the last example, old$ will contain
  111.  
  112.     a maximum of  10 significant  figures since  that is  the limit
  113.  
  114.     imposed by BASIC. 
  115.  
  116.  
  117.  
  118.          Having initialised a  variable it is then  possible to use
  119.  
  120.     any of the commands provided  by the library.  The commands are
  121.  
  122.     summarised in table  1 and comprise two main  forms.  The first
  123.  
  124.     type require  only one  string variable to  be passed  to them.
  125.  
  126.     Examples of these include : 
  127.  
  128.  
  129.  
  130.                                       Page 2
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.          neg$=FNNEG(pos$):REM neg=-pos 
  141.  
  142.          sinangle$=FNSIN(angle$):REM sinangle=SIN(angle) 
  143.  
  144.          inta$=FNINT(a$):REM inta=INT(a) 
  145.  
  146.  
  147.  
  148.          It is  possible to  include constants  that have  not been
  149.  
  150.     created using FNCONST as long  as the format described above is
  151.  
  152.     used.  The  following statements will achieve  exactly the same
  153.  
  154.     result: 
  155.  
  156.  
  157.  
  158.          pi$=FNACS(FNNEG(FNCONST("1"))) 
  159.  
  160.          pi$=FNACS("-1") 
  161.  
  162.  
  163.  
  164.          Note from the above example  that it is perfectly possible
  165.  
  166.     to nest the routines, ie the result of one routine being passed
  167.  
  168.     directly  to  another.  There  is  no  limit imposed  upon  the
  169.  
  170.     complexity of a structure except  the normal 250 character line
  171.  
  172.     length. 
  173.  
  174.  
  175.  
  176.          The second type of  instructions require two variables and
  177.  
  178.     perform the normal mathematical  processes such as addition and
  179.  
  180.     multiplication  and other  more complicated  functions such  as
  181.  
  182.     calculating polar angles from rectangular coordinates. They are
  183.  
  184.     used in the following way : 
  185.  
  186.  
  187.  
  188.          aplusb$=FNADD(a$,b$):REM aplusb=a+b 
  189.  
  190.          angle$=FNPOL(x$,y$):REM angle=ATN(y/x) 
  191.  
  192.  
  193.  
  194.  
  195.  
  196.                                       Page 3
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.          Since all the  numbers are handled as  normal strings then
  205.  
  206.     all  the normal  BASIC  commands may  be  used.  This  includes
  207.  
  208.     PRINT, LEFT$, RIGHT$ and MID$. 
  209.  
  210.  
  211.  
  212.          Two  other  important  facilities   are  provided  by  the
  213.  
  214.     routines.  These are FNamDP and FNSUIT.  FNamDP allows the user
  215.  
  216.     to set the  number of decimal places to be  returned (and value
  217.  
  218.     between 0 and 18) by the routines.  It is used in the following
  219.  
  220.     way : 
  221.  
  222.  
  223.  
  224.          past=FNamDP(14):REM Sets no. of decimal places to 14 and 
  225.  
  226.                              returns with past set to previous 
  227.  
  228.                              value. 
  229.  
  230.  
  231.  
  232.          The routine sets  the default number of  decimal places to
  233.  
  234.     18.  The  second function  FNSUIT is provided  for convenience.
  235.  
  236.     Since  all the  numbers  are  stored in  a  string  form it  is
  237.  
  238.     perfectly acceptable  to use  the VAL instruction  to determine
  239.  
  240.     the  value  of a  string.   However,  because of  the  extended
  241.  
  242.     accuracy  and  range,  some  numbers  will  be  unsuitable  for
  243.  
  244.     conversion to BASIC floating point variables and will result in
  245.  
  246.     a 'Number too  large' error.   To allow you  to determine  if a
  247.  
  248.     number  is  suitable  for  conversion  the  FNSUIT  command  is
  249.  
  250.     provided.  It  will return a  TRUE value  if the number  can be
  251.  
  252.     expressed by a BASIC variable and  FALSE if not.  It is used in
  253.  
  254.     the following way : 
  255.  
  256.  
  257.  
  258.          IF FNSUIT(extnum$) THEN a=VAL(extnum$) ELSE a=0 
  259.  
  260.  
  261.  
  262.                                       Page 4
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.          Note the use of a floating point basic variable and not an
  273.  
  274.     integer one which  would cause rounding to occur  if the number
  275.  
  276.     has a fraction part. 
  277.  
  278.  
  279.  
  280.          When using  the package  it is  best to  use a  mixture of
  281.  
  282.     extended  precision numbers  and normal  BASIC variables  where
  283.  
  284.     possible since the calculations with extended precision numbers
  285.  
  286.     are fairly slow  due to the complex encoding  and decoding that
  287.  
  288.     occurs with  every command.   It is advised  that they  only be
  289.  
  290.     used where  strictly necessary  (ie when extended  precision or
  291.  
  292.     range  is required).   Also,  only use  the  minimum number  of
  293.  
  294.     decimal  places required  since  extra  decimal places  require
  295.  
  296.     extra processing and so slow the package unnecessarily. 
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.                                       Page 5
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.     Table 1. Routines provided and their functions. 
  337.  
  338.  
  339.  
  340.          Routine name              Function 
  341.  
  342.          ==================================================== 
  343.  
  344.          num$=FNCONST("123")      | num$="123" 
  345.  
  346.          num$=FNNEG(num$)         | num$=-num$ 
  347.  
  348.          num$=FNABS(num$)         | num$=ABS(num$) 
  349.  
  350.          num$=FNINT(num$)         | num$=INT(num$) 
  351.  
  352.          num$=FNSQR(num$)         | num$=SQR(num$) 
  353.  
  354.          num$=FNLOG(num$)         | num$=LOG(num$) 
  355.  
  356.          num$=FNLN(num$)          | num$=LN(num$) 
  357.  
  358.          num$=FNEXP(num$)         | num$=EXP(num$) 
  359.  
  360.          num$=FNSIN(num$)         | num$=SIN(num$) 
  361.  
  362.          num$=FNCOS(num$)         | num$=COS(num$) 
  363.  
  364.          num$=FNTAN(num$)         | num$=TAN(num$) 
  365.  
  366.          num$=FNASN(num$)         | num$=ASN(num$) 
  367.  
  368.          num$=FNACS(num$)         | num$=ACS(num$) 
  369.  
  370.          num$=FNATN(num$)         | num$=ATN(num$) 
  371.  
  372.          num$=FNADD(a$,b$)        | num$=a$+b$ 
  373.  
  374.          num$=FNMUL(a$,b$)        | num$=a$*b$ 
  375.  
  376.          num$=FNSUB(a$,b$)        | num$=a$-b$ 
  377.  
  378.          num$=FNDIV(a$,b$)        | num$=a$/b$ 
  379.  
  380.          num$=FNPOW(a$,b$)        | num$=a$^b$ 
  381.  
  382.          num$=FNMOD(a$,b$)        | num$=a$ MOD b$ 
  383.  
  384.          num$=FNPOL(a$,b$)        | num$=ATN(b$/a$) 
  385.  
  386.          FNamDP(new)              | sets no. of dec. places 
  387.  
  388.          FNSUIT(num$)             | TRUE if BASIC can express it 
  389.  
  390.  
  391.  
  392.  
  393.  
  394.                                       Page 6
  395.  
  396.  
  397.